<?php

namespace App\Http\Admin\Controllers\Store;
use App\Services\store\StoreServices;
use App\Http\Controllers\Controller;
use App\Requests\StoreRequest;
use Hlcreation\Basic\Helper;

/**
 *   平台管理
 * */

class StoreController  extends Controller
{

    /**
     * @param UserInfoServices $services
     */
    public function __construct(StoreServices $services)
    {

        $this->services = $services;
    }

    /**
     * 平台列表
     */
    public function  index(){

        $userInfo = Helper::getPost('userinfo');
        $userInfo['limit']=  Helper::getPost('limit',20);
        $userInfo['name']=  Helper::getPost('name','');
        $page=Helper::getPost('page', 1);
        $userInfo['create_time']=Helper::getPost('create_time','');

        $list = $this->services->StoreList($userInfo);

        return Helper::toJson("平台列表", 200, $list);
    }


    /**
     * 创建平台
     */
    public function  save(StoreRequest $request){
           $userInfo = Helper::getPost('userinfo');

           $all = $request->all();
           $all['creator_uid']=$userInfo['user_info']['id'];
           $all['level']=$userInfo['user_info']['level'];
           $all['store_num']=$userInfo['user_info']['store_num'];
           $all['shop_num'] = $userInfo['user_info']['shop_num'];
           $data = $this->services->StoreSave($all);
           if($data){
               return Helper::toJson("创建成功", 200);
           }else{
               return Helper::toJson("创建失败", 400);
           }
    }

    /**
     * 获取平台信息
     */
    public  function  getInfo(){
        $id=Helper::getPost('store_id');
        $data = $this->services->getInfo($id);
        return Helper::toJson("数据获取成功", 200,$data);
    }


    /**
     * 修改平台
     */
    public  function  edit(StoreRequest $request){
            $userInfo = Helper::getPost('userinfo');
            $all = $request->all();
            $all['creator_uid']=$userInfo['user_info']['id'];
            $all['level']=$userInfo['user_info']['level'];
            $id=Helper::getPost('store_id');
            $data = $this->services->StoreEdit($id,$all);
            if($data){
                return Helper::toJson("修改成功", 200);
            }else{
                return Helper::toJson("修改失败", 400);
            }
    }

    //删除平台
    public  function  StoreDel(){

        $id=Helper::getPost('store_id');
        $admin_id= Helper::getPost('userinfo')['user_info']['id'];

        $data = $this->services->StoreDel($id,$admin_id);
        if($data){
            return Helper::toJson("删除成功", 200);
        }else{
            return Helper::toJson("删除失败", 400);
        }

    }

    /**
     * 禁用平台
     */
    public  function  EditDisabled(){
        $admin_id= Helper::getPost('userinfo')['user_info']['id'];
        $id=Helper::getPost('store_id');
        $type=Helper::getPost('type');
        if($type){
            $this->services->EditDisabled($admin_id,$id,1);
            return Helper::toJson("禁用成功", 200);
        }else{
            $this->services->EditDisabled($admin_id,$id,0);
            return Helper::toJson("取消禁用成功", 200);
        }


    }


    /**
     * 平台 配置开关
     */
    public  function  storeConfig(){
        $admin_id= Helper::getPost('userinfo')['user_info']['id'];
        $id=Helper::getPost('store_id');
        $type=Helper::getPost('type',1);
        $status=Helper::getPost('status',0);
        $this->services->storeConfig($admin_id,$type,$id,$status);
        return Helper::toJson("操作成功", 200);
    }

    /**
     * 进入电子合同系统
     */
    public  function  entryContract(){

            $store_id=Helper::getPost('store_id');

            $data=$this->services->entryContract($store_id);
            return $data;
    }


    /**
     * 同步短信次数和合同次数
     */
    public function   synchroSmsContract(){
            $store_id=Helper::getPost('store_id');

            $data=$this->services->synchroSmsContract($store_id);
            return Helper::toJson("同步成功", 200);

    }

    /***
     * 设置短信店铺次数
     */
    public  function  setSmsContract(){
        $admin_id= Helper::getPost('userinfo')['user_info']['id'];
        $store_id=Helper::getPost('store_id');
        //短信次数 店铺个数
        $arr['message_num']=Helper::getPost('message_num');
        $arr['shop_num']=Helper::getPost('shop_num');
        $data = $this->services->setSmsContract($arr,$store_id,$admin_id);
        if($data)  return Helper::toJson("设置成功", 200);
    }

}
